<template>
  <div class="app-container app-container-facilities-noise">
    <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="设施名称" prop="nsName">
                <el-input
                  v-model="queryParams.nsName"
                  placeholder="请输入设施名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="设施编号" prop="nsCode">
                <el-input
                  v-model="queryParams.nsCode"
                  placeholder="请输入设施编号"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="设施型号" prop="nsModel">
                <el-input
                  v-model="queryParams.nsModel"
                  placeholder="请输入设施型号"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="治理设施类型" prop="nsType">
                <el-select v-model="queryParams.nsType" placeholder="请选择治理设施类型" clearable size="small">
                  <el-option label="请选择字典生成" value="" />
                </el-select>
              </el-form-item>
              <el-form-item label="治理设施工艺" prop="nsCraft">
                <el-input
                  v-model="queryParams.nsCraft"
                  placeholder="请输入治理设施工艺"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="治理设施建设费用" prop="nsFacilityMoney">
                <el-input
                  v-model="queryParams.nsFacilityMoney"
                  placeholder="请输入治理设施建设费用"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="治理前噪声强度" prop="nsNoiseBefore">
                <el-input
                  v-model="queryParams.nsNoiseBefore"
                  placeholder="请输入治理前噪声强度"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="治理后噪声强度" prop="nsNoiseAfter">
                <el-input
                  v-model="queryParams.nsNoiseAfter"
                  placeholder="请输入治理后噪声强度"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="年运行(小时)" prop="nsYearTime">
                <el-input
                  v-model="queryParams.nsYearTime"
                  placeholder="请输入年运行(小时)"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="年运行费用(万元)" prop="nsYearMoney">
                <el-input
                  v-model="queryParams.nsYearMoney"
                  placeholder="请输入年运行费用(万元)"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="投入使用日期" prop="nsNewUseTime">
                <el-date-picker clearable size="small"
                  v-model="queryParams.nsNewUseTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择投入使用日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="环境监察记录" prop="nsEnvRecord">
                <el-input
                  v-model="queryParams.nsEnvRecord"
                  placeholder="请输入环境监察记录"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="运行规程" prop="nsRunRule">
                <el-input
                  v-model="queryParams.nsRunRule"
                  placeholder="请输入运行规程"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="设施平面布置图" prop="nsFacilityPicture">
                <el-input
                  v-model="queryParams.nsFacilityPicture"
                  placeholder="请输入设施平面布置图"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="运行记录" prop="nsRunRecord">
                <el-input
                  v-model="queryParams.nsRunRecord"
                  placeholder="请输入运行记录"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="企业统一信用代码" prop="epId">
                <el-input
                  v-model="queryParams.epId"
                  placeholder="请输入企业统一信用代码"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:noise:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:noise:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:noise:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['facilities:noise:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noiseList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="设施名称" prop="nsName"/>
      <el-table-column align="center" label="治理设施类型" prop="nsType"/>
      <el-table-column align="center" label="治理设施工艺" prop="nsCraft"/>
      <el-table-column align="center" label="治理设施建设费用" prop="nsFacilityMoney"/>
      <el-table-column align="center" label="治理前噪声强度(分贝)" prop="nsNoiseBefore"/>
      <el-table-column align="center" label="治理后噪声强度(分贝)" prop="nsNoiseAfter"/>
      <el-table-column align="center" label="年运行(小时)" prop="nsYearTime"/>
      <el-table-column align="center" label="年运行费用(万元)" prop="nsYearMoney"/>
      <el-table-column align="center" label="投入使用日期" prop="nsNewUseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nsNewUseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="环境监察记录" prop="nsEnvRecord"/>-->
      <!--      <el-table-column align="center" label="运行规程" prop="nsRunRule"/>-->
      <!--      <el-table-column align="center" label="设施平面布置图" prop="nsFacilityPicture"/>-->
      <!--      <el-table-column align="center" label="运行记录" prop="nsRunRecord"/>-->
      <!--      <el-table-column align="center" label="企业统一信用代码" prop="epId"/>-->
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['facilities:noise:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['facilities:noise:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改噪声治理设施对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="">

      <el-form ref="form" :model="form" :rules="rules" class="clearfix app-container-facilities-noise"
               label-width="180px" >
        <div class="content-left" style="width: 45%">
          <el-form-item label="设施名称" prop="nsName">
            <el-input v-model="form.nsName" placeholder="请输入设施名称"/>
          </el-form-item>
<!--          <el-form-item label="设施编号" prop="nsCode">
            <el-input v-model="form.nsCode" placeholder="请输入设施编号"/>
          </el-form-item>-->
          <el-form-item label="设施型号" prop="nsModel">
            <el-input v-model="form.nsModel" placeholder="请输入设施型号"/>
          </el-form-item>
          <el-form-item label="治理设施工艺" prop="nsCraft">
            <el-input v-model="form.nsCraft" placeholder="请输入治理设施工艺"/>
          </el-form-item>
          <el-form-item label="治理设施类型" prop="nsType">
            <!--            <el-select v-model="form.nsType" placeholder="请选择治理设施类型" style="width: 100%">-->
            <!--              <el-option label="请选择字典生成" value=""/>-->
            <!--            </el-select>-->
            <div style="width: 100%;">
              <el-select v-model="nsIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="nsChange">
                <el-option v-for="item in ns" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label="建设费用(万元)" prop="nsFacilityMoney">
            <el-input v-model="form.nsFacilityMoney" placeholder="请输入建设费用（万元）"/>
          </el-form-item>
          <el-form-item label="投入使用日期" prop="nsNewUseTime">
            <el-date-picker v-model="form.nsNewUseTime" clearable
                            placeholder="选择投入使用日期"
                            size="small"
                            style="width: 100%"
                            type="date"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
<!--          <el-form-item label="年运行(小时)" prop="nsYearTime">
            <el-input v-model="form.nsYearTime" placeholder="请输入年运行(小时)"/>
          </el-form-item>
          <el-form-item label="年运行费用(万元)" prop="nsYearMoney">
            <el-input v-model="form.nsYearMoney" placeholder="请输入年运行费用(万元)"/>
          </el-form-item>-->

        </div>
        <div class="content-left" style="width: 45%; float: right">
          <el-form-item label="治理前噪声强度(分贝)" prop="nsNoiseBefore">
            <el-input v-model="form.nsNoiseBefore" placeholder="请输入治理前噪声强度"/>
          </el-form-item>
          <el-form-item label="治理后噪声强度(分贝)" prop="nsNoiseAfter">
            <el-input v-model="form.nsNoiseAfter" placeholder="请输入治理后噪声强度"/>
          </el-form-item>
          <el-form-item label="环境监察记录" prop="nsEnvRecord">
            <FileUpload v-model="form.nsEnvRecord"/>
          </el-form-item>
          <el-form-item label="运行规程制度" prop="nsRunRule">
            <FileUpload v-model="form.nsRunRule"/>
          </el-form-item>
          <el-form-item label="设施平面布置图" prop="nsFacilityPicture">
            <ImageUpload v-model="form.nsFacilityPicture"/>
          </el-form-item>
          <el-form-item label="运行记录" prop="nsRunRecord">
            <FileUpload v-model="form.nsRunRecord"/>
          </el-form-item>
          <div class="line">
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addNoise, delNoise, exportNoise, getNoise, listNoise, updateNoise} from "@/api/facilities/noise";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isNotMUstTwoPoint,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Noise",
  data() {
    return {
      ns: [
        {
          value: '0',
          label: '隔音墙'
        },
        {
          value: '1',
          label: '吸音板'
        },
        {
          value: '2',
          label: '减振装置'
        }
      ],
      nsIndex: '0',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 噪声治理设施表格数据
      noiseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nsName: null,
        nsCode: null,
        nsModel: null,
        nsType: null,
        nsCraft: null,
        nsFacilityMoney: null,
        nsNoiseBefore: null,
        nsNoiseAfter: null,
        nsYearTime: null,
        nsYearMoney: null,
        nsNewUseTime: null,
        nsEnvRecord: null,
        nsRunRule: null,
        nsFacilityPicture: null,
        nsRunRecord: null,
        epId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nsName: [
          {required: true, message: "设施名称不能为空", trigger: "blur"}
        ],
        nsFacilityMoney: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
        nsYearMoney: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    nsChange(e) {
      this.form.nsType = e;
    },
    /** 查询噪声治理设施列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listNoise(this.queryParams).then(response => {
            this.noiseList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        nsId: null,
        nsName: null,
        nsCode: null,
        nsModel: null,
        nsType: null,
        nsCraft: null,
        nsFacilityMoney: null,
        nsNoiseBefore: null,
        nsNoiseAfter: null,
        nsYearTime: null,
        nsYearMoney: null,
        nsNewUseTime: null,
        nsEnvRecord: null,
        nsRunRule: null,
        nsFacilityPicture: null,
        nsRunRecord: null,
        epId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.nsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加噪声治理设施";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const nsId = row.nsId || this.ids
      getNoise(nsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改噪声治理设施";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.nsId != null) {
            updateNoise(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addNoise(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nsIds = row.nsId || this.ids;
      this.$confirm('是否确认删除噪声治理设施编号为"' + nsIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delNoise(nsIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有噪声治理设施数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportNoise(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
<style scoped>


</style>
